package com.own.testapp.module;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.own.testapp.R;
import com.own.testapp.base.BaseNetActivity;
import com.own.testapp.model.MyResponse;
import com.own.testapp.utils.TextUtil;
import com.own.testapp.utils.ToastUtils;
import com.own.testapp.utils.UIHelper;

import okhttp3.Call;
import okhttp3.Response;

public class LoginActivity extends BaseNetActivity
{
    private EditText et_user_login,et_psw_login;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        et_user_login = (EditText) findViewById(R.id.et_user_login);
        et_psw_login = (EditText) findViewById(R.id.et_psw_login);
    }

    /**
     * 用户注册
     * @param view
     */
    public void onRegister(View view){
        UIHelper.startActivity(mCotext,RegisterActivity.class);
    }

    /**
     * 用户登录
     * @param view
     */
    public void onLogin(View view){
        String userContent = et_user_login.getText().toString();
        String pswContent =  et_psw_login.getText().toString();
        if (TextUtil.isEmpty(userContent) || TextUtil.isEmpty(pswContent)){
            ToastUtils.showShort(mCotext,"用户名或密码不能为空");
            return;
        }
        getDataFromNet(null,this,0);
    }

    @Override
    public void handleSuccess(String data, Call call, Response response, int type)
    {
        super.handleSuccess(data, call, response, type);
        if (type == 0){
            Gson gson = new Gson();
            MyResponse myResponse = gson.fromJson(data,MyResponse.class);
            Bundle mBundle = new Bundle();
            mBundle.putSerializable("data",myResponse);
            UIHelper.startActivityWithData(mCotext,MainActivity.class,mBundle);
            finish();
        }
    }

    @Override
    public void handleError(Call call, Response response, int type)
    {
        super.handleError(call, response, type);
    }
}
